package com.study.d015propertybind.fx;

import javafx.application.Application;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.binding.IntegerBinding;
import javafx.beans.binding.NumberBinding;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
*@description 绑定计算
*@author xtayhame
*date 2020年3月5日
*version
*since v1.0
*/
public class BindCacleMain extends Application{
	public static void main(String[] args) {


		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		SimpleIntegerProperty a = new SimpleIntegerProperty(1);
		SimpleIntegerProperty b = new SimpleIntegerProperty(2);
		IntegerBinding c= a.add(5);
		System.out.println(c.get());  //6

		a.set(10);
		System.out.println(c.get());  //15

		NumberBinding c2= a.add(b);
		System.out.println(c2.intValue());  // 12

		b.set(15);
		System.out.println(c2.intValue());  // 25


		TextField add1Field = new TextField();
		TextField add2Field = new TextField();
		TextField resultField = new TextField();
		Button  calcBtn = new Button("计算");






		//例子二： 输入数字，让button组件的宽和高随着输入值的的大小进行变化
		AnchorPane anchorPane = new AnchorPane();
		Button b1 = new Button();
		TextField t1 = new TextField();

		anchorPane.getChildren().addAll(b1,t1);

		stage.setScene(new Scene(anchorPane, 400, 500));
		stage.show();

		b1.setPrefWidth(anchorPane.getWidth()/2);
		b1.setPrefHeight(anchorPane.getHeight()/2);

		SimpleIntegerProperty  x= new SimpleIntegerProperty(3);
		//注意：这里用的是widthProperty() ，如果用prefWidthProperty() 则无效
		DoubleBinding  dbw= anchorPane.widthProperty().divide(x);
		DoubleBinding  dbh= anchorPane.heightProperty().divide(x);

		t1.textProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				try {
					System.out.println(newValue);
					int num = Integer.parseInt(newValue);
					x.set(num);
				} catch (NumberFormatException e) {
					return;
				}
			}
		});

		b1.prefWidthProperty().bind(dbw);
		b1.prefHeightProperty().bind(dbh);




		/*****************通过使用绑定计算两个输入框的值***************************/
		Label addLabel = new Label("+");
		Label eqLabel = new Label("=");
		anchorPane.getChildren().addAll(add1Field, add2Field,resultField,calcBtn,addLabel,eqLabel);
		add1Field.setPrefWidth(50);
		add2Field.setPrefWidth(50);
		resultField.setPrefWidth(50);



		AnchorPane.setTopAnchor(add1Field, 300.0);
		AnchorPane.setTopAnchor(addLabel, 300.0);
		AnchorPane.setTopAnchor(add2Field, 300.0);
		AnchorPane.setTopAnchor(eqLabel, 300.0);
		AnchorPane.setTopAnchor(resultField, 300.0);
		AnchorPane.setTopAnchor(calcBtn, 300.0);

		AnchorPane.setLeftAnchor(add1Field, 10.0);
		AnchorPane.setLeftAnchor(addLabel, 60.0);
		AnchorPane.setLeftAnchor(add2Field, 80.0);
		AnchorPane.setLeftAnchor(eqLabel, 140.0);
		AnchorPane.setLeftAnchor(resultField, 180.0);
		AnchorPane.setLeftAnchor(calcBtn, 240.0);

		SimpleIntegerProperty add1Property = new SimpleIntegerProperty();
		SimpleIntegerProperty add2Property = new SimpleIntegerProperty();

		NumberBinding nb= add1Property.add(add2Property);

		calcBtn.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				add1Property.set(Integer.parseInt(add1Field.getText()));
				add2Property.set(Integer.parseInt(add2Field.getText()));
				resultField.setText(String.valueOf(nb.intValue()));
			}
		});



	}


}
